﻿/*
 日志打印定义
    debug   数据库sql语句处理信息   页面测试语句
    info    页面流程脚本
    warn    一般空的异常情况
    error   数据库连接失败  通信失败
    fatal   程序启动失败
 */


using System;
using System.IO;
using System.Diagnostics;
namespace System
{
    /// <summary>
    /// 日志管理类
    /// </summary>
    public static class LogManager
    {
        private static string logPath = string.Empty;
        private static int logLevel = 0;
        private static string levelstring = System.Configuration.ConfigurationManager.AppSettings["loglevel"];

        /// <summary>
        /// 自定义打印级别  Warning,DeBug,Error,Run
        /// </summary>
        public static int LogLevel
        {
            get
            {
                if (string.IsNullOrEmpty(levelstring))
                {
                    return 0;
                }
                else
                {
                    int levelint = Convert.ToInt32(levelstring);
                    return levelint;
                }
            }
            set { logLevel = value; }
        }

        /// <summary>
        /// 保存日志的文件夹
        /// </summary>
        public static string LogPath
        {
            get
            {
                if (logPath == string.Empty)
                {
                    logPath = AppDomain.CurrentDomain.BaseDirectory + @"Log\";//这个没有尝试过，应该没有什么错误
                    if (!Directory.Exists(logPath))
                    {
                        Directory.CreateDirectory(logPath);
                    }
                }
                return logPath;
            }
            set { logPath = value; }
        }

        private static string logFielPrefix = string.Empty;
        /// <summary>
        /// 日志文件前缀
        /// </summary>
        public static string LogFielPrefix
        {
            get
            {

                if (string.IsNullOrEmpty(logFielPrefix))
                {
                    logFielPrefix = "WebSiteLog";
                }
                return logFielPrefix;
            }
            set { logFielPrefix = value; }
        }

        /// <summary>
        /// 写日志
        /// </summary>
        public static void WriteLog(string logFile, string msg)
        {
            if (!string.IsNullOrEmpty(msg))
            {
                int lev = 0;
                if (logFile.ToLower().Equals("debug"))
                {
                    lev = 1;
                }
                else if (logFile.ToLower().Equals("info"))
                {
                    lev = 2;
                }
                else if (logFile.ToLower().Equals("warn"))
                {
                    lev = 3;
                }
                else if (logFile.ToLower().Equals("error"))
                {
                    lev = 4;
                }
                else
                {
                    lev = 5;
                }

                if (lev >= LogLevel)
                {
                    string msgstring = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss: ") + logFile + "  " + msg;
                    Common.IO.File.WriteFile(LogPath + LogFielPrefix + " " + DateTime.Now.ToString("yyyyMMdd") + ".Log", msgstring);
                }

            }
        }

        /// <summary>
        /// 数据库sql语句处理信息   页面测试语句
        /// </summary>
        /// <param name="msg"></param>
        public static void Debug(this string msg)
        {
            WriteLog("debug", msg);
        }
        /// <summary>
        /// 页面流程脚本
        /// </summary>
        /// <param name="msg"></param>
        public static void Info(this string msg)
        {

            WriteLog("info", msg);

        }
        /// <summary>
        /// 一般空的异常情况
        /// </summary>
        /// <param name="msg"></param>
        public static void Warn(this string msg)
        {

            WriteLog("warn", msg);

        }
        /// <summary>
        /// 数据库连接失败  通信失败
        /// </summary>
        /// <param name="msg"></param>
        public static void Error(this string msg)
        {

            WriteLog("error", msg);

        }
        /// <summary>
        /// 程序启动失败
        /// </summary>
        /// <param name="msg"></param>
        public static void Fatal(this string msg)
        {

            WriteLog("fatal", msg);

        }

    }
    /// <summary>
    /// 日志类型
    /// </summary>
    internal enum LogFile
    {
        debug = 1,
        info = 2,
        warn = 3,
        error = 4,
        fatal = 5
    }
}
